<template>
  <div class="popup">
    <form action="/">
      <van-search
        v-model="searchVal"
        show-action
        :placeholder="inpVal"
        @search="onSearch"
        @input="onInput"
        @cancel="onCancel"
      />
    </form>
    <HistoryHot
      v-if="blockShow == 1"
      :historyKeywordList="historyKeywordList"
      :hotKeywordList="hotKeywordList"
      @tagClick="tagClick"
    ></HistoryHot>
    <MyList
      v-else-if="blockShow == 2"
      :list="list"
      @cellClick="tagClick"
    ></MyList>
    <MyProduct
      v-else
      :goodsList="goodsList"
      :filterCategory="filterCategory"
      @categoryChange="categoryChange"
      @priceChange="priceChange"
    ></MyProduct>
  </div>
</template>
<script>
import {
  getPopupData,
  getTimeData,
  getGoodsData,
  getGoodsListData,
} from "@/request/api.js";
import HistoryHot from "@/components/HistoryHot.vue";
import MyList from "@/components/MyList.vue";
import MyProduct from "@/components/MyProduct.vue";
export default {
  components: {
    HistoryHot,
    MyList,
    MyProduct,
  },
  data() {
    return {
      //输入框的值
      searchVal: "",
      //占位符
      inpVal: "",
      //显示区块
      //1、热门历史区块
      //2、列表区块
      //3、搜索区块
      blockShow: 1,
      //历史搜索
      historyKeywordList: [],
      //热门搜索
      hotKeywordList: [],
      //列表
      list: [],
      //搜索参数
      order: "desc",
      categoryId: "0",
      sort: "id",
      //分类下拉菜单
      filterCategory: [],
      //商品列表
      goodsList: [],
      //每页数量
      pageSize: 0,
      //总页数
      totalPages: 0,
      //当前页
      currentPage: 1,

      page: 1,
      size: 1000,
    };
  },
  watch: {
    searchVal(newVal, oldVal) {
      if (newVal.length == 0) {
        this.blockShow = 1;
      }
    },
  },
  methods: {
    //搜索事件
    onSearch() {
      getGoodsData({
        keyword: this.searchVal,
        page: 1,
        size: 20,
        order: this.order,
        categoryId: this.categoryId,
        sort: this.sort,
      }).then((res) => {
        if (res.errno == 0) {
          let { filterCategory, goodsList, pageSize, totalPages, currentPage } =
            res.data;
          let arr = JSON.parse(
            JSON.stringify(filterCategory)
              .replace(/name/g, "text")
              .replace(/id/g, "value")
          );
          this.filterCategory = arr;
          this.goodsList = goodsList;
          this.pageSize = pageSize;
          this.totalPages = totalPages;
          this.currentPage = currentPage;
          this.blockShow = 3;
        }
      });
    },
    //实时搜索
    onInput() {
      getTimeData({
        keyword: this.searchVal,
      }).then((res) => {
        this.blockShow = 2;
        this.list = res.data;
        this.onSearch();
      });
    },
    tagClick(val) {
      this.searchVal = val;
      this.onSearch();
    },
    categoryChange(val) {
      console.log(val);
      this.categoryId = val;
      this.onSearch();
    },
    priceChange(val) {
      this.order = val;
      this.sort = "price";
      this.onSearch();
    },
    //取消事件
    onCancel() {
      this.$router.go(-1);
    },
  },
  created() {
    let id = this.$route.query.cid;
    if (id) {
      this.blockShow = 3;
      getGoodsListData({
        categoryId: id,
        page: this.page,
        size: this.size,
      }).then((res) => {
        if (res.errno == 0) {
          let { filterCategory, goodsList, pageSize, totalPages, currentPage } =
            res.data;
          let arr = JSON.parse(
            JSON.stringify(filterCategory)
              .replace(/name/g, "text")
              .replace(/id/g, "value")
          );
          this.filterCategory = arr;
          this.goodsList = goodsList;
          this.pageSize = pageSize;
          this.totalPages = totalPages;
          this.currentPage = currentPage;
          // setTimeout(() => {
          //   location.reload();
          // }, 100);
        }
      });
    } else {
      //搜索窗口数据
      getPopupData().then((res) => {
        if (res.errno == 0) {
          let { defaultKeyword, historyKeywordList, hotKeywordList } = res.data;
          this.inpVal = defaultKeyword.keyword;
          this.historyKeywordList = historyKeywordList;
          this.hotKeywordList = hotKeywordList;
          this.blockShow = 1;
        }
      });
    }
  },
};
</script>
<style lang="less" scoped>
.popup {
  width: 100%;
  height: 100%;
  position: absolute;
  right: 0;
  top: 0;
  z-index: 1000;
  background-color: #efefef;
}
.van-search {
  position: sticky;
  top: 0;
  z-index: 99;
}
</style>